﻿using Abp.Domain.Repositories;
using Abp.UI;
using AbpDemo.Entity;
using study.Employees.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study.Employees
{
    public class EmployeeAppService : studyAppServiceBase, IEmployeeAppservice
    {
        private readonly IRepository<Employee> _employeeRepository;
        public EmployeeAppService(IRepository<Employee> employeeepository)
        {
            _employeeRepository = employeeepository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        public void CreateEmployee(CreateEmployeeDto input)
        {
            var task = new Employee() { Code = input.Code, Name = input.Name };
            _employeeRepository.Insert(task);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        public void DelEmployee(EmployeeDto input)
        {
            var task = new Employee() { Id = input.Id, Code = input.Code, Name = input.Name };
            _employeeRepository.Delete(task);
        }
        /// <summary>
        /// 通过id获取name
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetEmployeeName(int id)
        {
            var taskAll = _employeeRepository.Get(id).Name;
            return taskAll;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<Employee> OutPutAllEmployee(EmployeeDto input)
        {
            var employeeAll = _employeeRepository.GetAll();
            if (!string.IsNullOrEmpty(input.Keyword))
            {
                employeeAll = employeeAll.Where(c => c.Code.Contains(input.Keyword) || c.Name.Contains(input.Keyword));
            }

            return employeeAll.ToList();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        public void UpdateEmployee(EmployeeDto input)
        {
            var allList = _employeeRepository.GetAll().Where(c => c.Code == input.Code && c.Id != input.Id);
            if (allList.Count() > 0)
            {
                throw new UserFriendlyException("提示", "该员工编号已存在，请重新输入");
            }
            var task = new Employee() { Id = input.Id, Code = input.Code, Name = input.Name };
            _employeeRepository.Update(task);

        }
    }
}
